package mysql.cs.packet;

import common.Util.ByteUtil;
import mysql.MysqlPacket;
import mysql.cs.prop.Capabilities;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * int<1>	header	[fe] EOF header
 * if capabilities & CLIENT_PROTOCOL_41 {
 *   int<2>	warnings	number of warnings
 *   int<2>	status_flags	Status Flags
 * }
 * @ClassName EOFPacket
 * @Description TODO
 * @Author fengk
 * @Date 2018/8/29 00:00
 */

// TODO: 2018/8/29 没有进行CLIENT_PROTOCOL_41的判断
//CLIENT_PROTOCOL_41 默认
public class EOFPacket extends MysqlPacket {
    private byte header;
    private int numOfWarn;
    private int statusFlags;
    public void encode(byte[] data){
        int index=0;
        header=data[index];
        index++;
        if((Capabilities.CLIENT_PROTOCOL_41&Capabilities.CAPABILITIES) != (long)0x00){
            numOfWarn= ByteUtil.readUnsignedShortLittleEndian(data,index);
            index+=2;
            statusFlags=ByteUtil.readUnsignedShortLittleEndian(data,index);
            //(^--^)
            index+=2;
        }

    }
    public byte[] decode() throws IOException{
        ByteArrayOutputStream out=new ByteArrayOutputStream(5);
        out.write(header);
        if((Capabilities.CLIENT_PROTOCOL_41&Capabilities.CAPABILITIES) != (long)0x00){
            ByteUtil.writeUnsignedShortLittleEndian(numOfWarn,out);
            ByteUtil.writeUnsignedShortLittleEndian(statusFlags,out);
        }

        return out.toByteArray();
    }

}
